"""
认证工具函数
"""
from flask import current_app, jsonify
from flask_jwt_extended import create_access_token, create_refresh_token
from datetime import timedelta

def generate_tokens(user_id):
    """
    为用户生成访问令牌和刷新令牌
    
    Args:
        user_id: 用户ID
        
    Returns:
        包含访问令牌和刷新令牌的字典
    """
    # 创建访问令牌，有效期默认为1小时
    access_token = create_access_token(
        identity=user_id,
        expires_delta=timedelta(hours=1)
    )
    
    # 创建刷新令牌，有效期默认为30天
    refresh_token = create_refresh_token(
        identity=user_id,
        expires_delta=timedelta(days=30)
    )
    
    return {
        'access_token': access_token,
        'refresh_token': refresh_token
    }

def error_response(message, status_code=400):
    """
    生成错误响应
    
    Args:
        message: 错误消息
        status_code: HTTP状态码
        
    Returns:
        JSON错误响应
    """
    response = jsonify({
        'error': True,
        'message': message
    })
    response.status_code = status_code
    return response
